﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Base_Complementary_Encryption
{
    public partial class Login : Form
    {
        public string g = "";
        public bool mainCaller = false;

        public Login()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            System.Windows.Forms.Control.CheckForIllegalCrossThreadCalls = false;
        }

        public void StatusGetter()
        {
            try
            {
                g = MethodBox.WebServices.CommonAPI.RequestData("http://mb.cgbtz.gay/api/Login.php",
                    $"username={textBox1.Text}&password={textBox2.Text}", new List<string>() { });

            }
            catch (Exception ex)
            {
                label5.ForeColor = Color.Red;
                label5.Text = ex.Message;
                return;
            }
            if (g == "")
            {
                MessageBox.Show("用户名或密码错误！", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            else if (g.StartsWith("%"))
            {
                MessageBox.Show("欢迎您", "成功", MessageBoxButtons.OK, MessageBoxIcon.Information);
                CallMain();
            }
            else if (g.StartsWith("$"))
            {
                MessageBox.Show(g.Substring(1), "账户无法使用", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            else
            {
                MessageBox.Show(g, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        public void CallMain()
        {

            mainCaller = true;
        }

        private void btn_Login_Click(object sender, EventArgs e)
        {
            label5.ForeColor = Color.Green;
            label5.Text = "加载中，请稍后......";

            Thread LoginThread = new Thread(new ThreadStart(StatusGetter));
            LoginThread.Start();
        }

        private void timer1_Tick(object sender, EventArgs e)
        {

            if (mainCaller)
            {
                Main main = new Main();
                main.Show();
            }
            if (mainCaller)
            {
                mainCaller = !mainCaller;
                Hide();
            }
        }

        private void panel1_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            Main mn = new Main();
            mn.Show();
        }
    }
}
